//获取应用实例
const app = getApp();
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var pro = []
Page({
  data: {
    region: ['请选择所在地区','','']
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          'Height': res.windowHeight
        })
      }
    })
    if (options.aid != undefined) {
      this.setData({aid: options.aid})
      wx.setNavigationBarTitle({ title: '修改地址' });
      that.getAddress();
    } else {
      wx.setNavigationBarTitle({ title: '添加地址' })
    }
  },
  onShow: function (options) {

  },


  //选择微信地址
  WXAddress: function () {
    var that = this;
    wx.chooseAddress({
      success: function (res) {
        that.setData({
          userName: res.userName,
          region: [res.provinceName, res.cityName, res.countyName],
          detailInfo: res.detailInfo,
          telNumber: res.telNumber
        })
      },
      fail: function () {
        util.showError("失败");
      }
    })
  },
//选择省市区
  selectAddress:function(e){
    var value = e.detail.value;
    this.setData({
      region:value
    })
  },
  //检测提示信息
  checkAddressPrompt:function(message){
    wx.showToast({
      title: message,
      image: '../../images/icon_33.png',
      duration: 1500
    })
  },
  checkAddress: function () {
    var that = this;
    if (this.data.userName == '' || this.data.userName == undefined) {
      util.showError("收货人不能为空");
      return false
    }

    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.telNumber))) {
      util.showError("手机号不正确");
      return false
    }

    if (this.data.detailInfo == '' || this.data.detailInfo == undefined) {
      util.showError("收货地址不正确");
      return false
    }
    var aid = this.data.aid;
    if (aid) {
      this.editAddress()
    } else {
      this.addAddress()
    }

  },
  modifyName: function (e) {  //用户名
    var name = e.detail.value;
    this.setData({
      userName: name
    })
  },
  modifyPhone: function (e) {  //手机号
    var telNumber = e.detail.value;
    this.setData({
      telNumber: telNumber
    })
  },
  modifyAddress: function (e) {  //详细地址
    var address = e.detail.value;
    this.setData({
      detailInfo: address
    })
  },
 

//获取地址
  getAddress: function () {
    var that = this;
    api.POST({
      params: { aid :that.data.aid},
      url: app.globalData.base_url + "address/info",
      success: function (res) {
        if (res.data.code == '') {
          var resData = res.data.data;
          that.setData({
            userName: resData.username,
            telNumber: resData.mobile,
            detailInfo: resData.address,
            region: [resData.province.name, resData.city.name, resData.town.name]
          })
        }else{
          util.showError(res.data.message);
        }
      }
    })
  },
  //添加地址
  addAddress: function () {
    var that=this;
    var params = {
      username: that.data.userName,
      mobile: that.data.telNumber,
      province: that.data.region[0],
      city: that.data.region[1],
      town: that.data.region[2],
      address: that.data.detailInfo,
      is_default: 1
    };
    api.POST({
      params: params,
      url: app.globalData.base_url +"address/add",
      success: function (res) {
        if (res.data.code == '') {
          wx.navigateBack({
            delta: 1
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  editAddress: function () {  //修改地址
  var that=this;
    var params = {
      aid: that.data.aid,
      username: that.data.userName,
      mobile: that.data.telNumber,
      province: that.data.region[0],
      city: that.data.region[1],
      town: that.data.region[2],
      address: that.data.detailInfo,
      is_default: 1
    };
    api.POST({
      params: params,
      url: app.globalData.base_url + "address/modify",
      success: function (res) {
        if (res.data.code == '') {
          wx.navigateBack({
            delta: 1
          })
        } else {
          util.showError(res.data.message);
        }
      }
    })

  }
})